layui.use(['element', 'table', 'laytpl'], function () {
    var $ = layui.$,
        table = layui.table,
        layer = layui.layer;
    tableIns =  table.render({
        elem: '#demo'
        , id: "mytable"
        , url: '/employee/list' //数据接口
        , page: true //开启分页
        , limit: 15
        , limits: [15, 30, 60, 100]
        , response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            , statusCode: 200 //成功的状态码，默认：0
            , msgName: 'msg' //状态信息的字段名称，默认：msg
            , countName: 'total' //数据总数的字段名称，默认：count
            , dataName: 'rows' //数据列表的字段名称，默认：data
        }
        , cols: [[ //表头
            {title: '序号', type: 'numbers'}
            , {field: 'name', title: '姓名'}
            , {field: 'idNo', title: '身份证'}
            , {field: 'departmentName', title: '部门'}
            , {field: 'postName', title: '职位'}
            , {title: '是否能登陆系统',templet:function (d) {
                    var flag=false;
                    if (d.userId!=null){
                        $.ajax({
                            url:"/user/selectUser",
                            dataType:"json",
                            type:"post",
                            async:false,
                            data:{id:d.userId},
                            success:function(rs){
                                if(rs.code == 200 ){
                                    var date   = rs.data;
                                    if (date.status==1) {
                                        flag=true;
                                    }
                                }
                            }
                        });
                    }
                    if (flag){
                        return "是"
                    } else{
                        return "否"
                    }
                }}
            , {title: '操作', toolbar: "#toolbar"}
        ]]
    });

    //工具条
    table.on("tool(test)", function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        if ("edit" === layEvent) {
            location.href = "/employee/edit?id=" + data.id;
        } else if ("delete" === layEvent) {
            location.href = "/employee/leaveOffice?id=" + data.id;
        } else if ("loginSetting" === layEvent) {
            zz = layer.open({
                type: 2,
                area: ['650px', '480px'],
                content: "/employee/userAddUI?id=" + data.id
            });
        }
    });

    //批量删除
    $("#deleteAll").on('click', function () {
        var checkStatus = layui.table.checkStatus('mytable'); //test即为基础参数id对应的值
        if (checkStatus.data.length < 1) return false;
        var params = '';
        $.each(checkStatus.data, function (i, v) {
            params += "id=" + v.id + "&";
        });
        //删除
        location.href = "/employee/delete?" + params;

    });

    //导出excel
    $("#exportExcel").on('click', function () {
        location.href = "/employee/export/excel";
    });

    //初始化菜单
    var $menu = $("#menu");
    $menu.menu();

    var zTreeObj;
    // zTree 的参数配置
    var setting = {
        view: {
            showIcon: false,//设置是否显示节点图标
            showLine: false,//不显示连接线，默认值true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onRightClick: onRightClick,
            onClick: zTreeOnClick
        }
    };


    $.ajax({
        url: "/department/tree",
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                $.fn.zTree.init($("#DepartmentTree"), setting, data.data);
            }
            if (data.data.code == 0) {
                layer.msg(data.message)
            }
        },
        error: function (e) {
            console.log("部门树加载失败");
        }
    });

    function zTreeOnClick(event, treeId, treeNode) {
        if (treeNode==null || treeNode==undefined||treeNode==""){
            return;
        }
        table.render({
            elem: '#demo'
            , id: "mytable"
            , url: '/employee/list?departmentId='+treeNode.id//数据接口
            , page: true //开启分页
            , limit: 15
            , limits: [15, 30, 60, 100]
            , response: {
                statusName: 'code' //数据状态的字段名称，默认：code
                , statusCode: 200 //成功的状态码，默认：0
                , msgName: 'msg' //状态信息的字段名称，默认：msg
                , countName: 'total' //数据总数的字段名称，默认：count
                , dataName: 'rows' //数据列表的字段名称，默认：data
            }
            , cols: [[ //表头
                {title: '序号', type: 'numbers'}
                , {field: 'name', title: '姓名'}
                , {field: 'idNo', title: '身份证'}
                , {field: 'departmentName', title: '部门'}
                , {field: 'postName', title: '职位'}
                , {title: '是否能登陆系统',templet:function (d) {
                    var flag=false;
                    if (d.userId!=null){
                        $.ajax({
                            url:"/user/selectUser",
                            dataType:"json",
                            type:"post",
                            async:false,
                            data:{id:d.userId},
                            success:function(rs){
                                if(rs.code == 200 ){
                                    var date   = rs.data;
                                    if (date.status==1) {
                                        flag=true;
                                    }
                                }
                            }
                        });
                    }
                    if (flag){
                        return "是"
                    } else{
                        return "否"
                    }
                    }}
                , {title: '操作', toolbar: "#toolbar"}
            ]]
        });
    }
    function onRightClick(event, treeId, treeNode) {
        if (treeNode==null || treeNode==undefined||treeNode==""){
            return;
        }
        if (treeNode.id == "0") {
            if (!treeNode.children){
                layer.msg("请您先创建部门，将从业人员添加至各部门下")
            }

        }
        if (treeNode.id != "0") {
            var $menu = $("#menu");
            // 鼠标点击位置坐标
            var mouseX = event.pageX;
            var mouseY = event.pageY;
            // ul菜单出现
            $menu.css({
                "left": mouseX,
                "top": mouseY
            }).show();
            $(".save").attr("href", "/employee/add?id=" + treeNode.id);
        }
    }


    $menu.mouseleave(function(){
        $("#menu").hide()
    });
});